var devCode=getQueryString("devCode");
var devId=getQueryString("devId");
var devName=getQueryString("devName");
var url='../devleaverecord/list'
if(devId){
   url += '?devId=' + devId;
}
if (devCode!=null){
    url= '../devleaverecord/list?devCode='+devCode+'&status=2'
}
$(function () {
    $("#jqGrid").Grid({
        url: url,
        colModel: [

            {label: '设备名称', name: 'devName', index: 'DEV_NAME', width: 80},
            {label: '设备编码', name: 'devCode', index: 'DEV_CODE', width: 80},
            {label: '闲置时间', name: 'createDate', index: 'createDate', width: 80,
                formatter: function (value) {
                    return transDate(value, "yyyy-MM-dd");
                }
            },
            {label: '设备型号', name: 'devModel', index: 'DEV_MODEL', width: 80},
            // {label: '数量', name: 'devNum', index: 'DEV_NUM', width: 80},
            // {
            //     label: '启用日期', name: 'startDate', index: 'START_DATE', width: 80, formatter: function (value) {
            //         return transDate(value, 'yyyy-MM-dd')
            //     }
            // },
            {label: '原值', name: 'origVal', index: 'ORIG_VAL', width: 80},
            {label: '净值', name: 'netVal', index: 'NET_VAL', width: 80},
            // {label: '使用单位', name: 'useUnit', index: 'USE_UNIT', width: 80},
            {label: '所属车间', name: 'useUnit', index: 'useUnit', width: 80},
            {label: '地点', name: 'address', index: 'ADDRESS', width: 80},
            {label: '原因', name: 'reson', index: 'RESON', width: 80},
            {label: '状态', name: 'status', width: 80,
                formatter:function(value, col, row){
                    return sysEnums.DEV_LEAVE_STATUS[row.status];
                }
            },
            {label: 'id', name: 'id', index: 'ID', key: true, hidden: true},
            {label: '设备ID', name: 'devId', index: 'DEV_ID', hidden: true}]
    });
});

var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        devLeaveRecord: {
            reson: '',
            status: '',
            address: '',
            useUnit: '',
            netVal: '',
            origVal: '',
            startDate: '',
            devNum: '',
            devModel: '',
            devId: '',
            devName: '',
            devCode: '',
            assertCode: '',
            delFlag: '',
            id: '',
            updateDate: '',
            updateUser: '',
            createDate: '',
            createUser: '',
        },
        ruleValidate: {
            devCode: [
                {required: true, message: '设备编码不能为空', trigger: 'blur'}
            ],
            devNum: [
                {required: true, message: '数量不能为空', trigger: 'blur'}
            ],
        },
        q: {
            devName: '',
            devProductName:'',
            devProceName:''
        },
        statusList: enumsToList('STATUS2'),
        List:{},
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.devLeaveRecord = {
                reson: '',
                address: '',
                useUnit: '',
                netVal: '',
                origVal: '',
                startDate: '',
                devNum: '',
                devModel: '',
                devId: '',
                devName: '',
                devCode: '',
                assertCode: '',
                delFlag: '1',
                id: '',
                updateDate: '',
                updateUser: '',
                createDate: '',
                createUser: '',
            };
            if (devCode!=null) {
                vm.devLeaveRecord.devCode = devCode;
                vm.devLeaveRecord.devName =devName;
                vm.devLeaveRecord.devId = devId;
            }
        },
        update: function (event) {
            var id = getSelectedRow("#jqGrid");
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id);
        },
        saveOrUpdate: function (event) {

            var url = vm.devLeaveRecord.id == (null || '') ? "../devleaverecord/save" : "../devleaverecord/update";
            Ajax.request({
                url: url,
                params: JSON.stringify(vm.devLeaveRecord),
                type: "POST",
                contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
            });
        },
        del: function (event) {
            var ids = getSelectedRows("#jqGrid");
            if (ids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                Ajax.request({
                    url: "../devleaverecord/delete",
                    params: JSON.stringify(ids),
                    type: "POST",
                    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });
            });
        },
        getInfo: function (id) {
            Ajax.request({
                url: "../devleaverecord/info/" + id,
                async: true,
                successCallback: function (r) {
                    vm.devLeaveRecord = r.devLeaveRecord;
                }
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: vm.q,
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
        },
        reloadSearch: function () {
            vm.q = {
                devName: '',
                devProductName:'',
                devProceName:''
            };
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        },
        //显示设备列表-使用设备码
        getDeviceInfo: function () {
            openWindow({
                type: 2,
                title: '<i class="fa fa-file-text-o"></i> 选择设备',
                content: '../sys/deviceInfoSelect.html',
                btn: ['确定', '取消'],
                btn1: function (index) {
                    var iframeWin = window["layui-layer-iframe" + index];
                    var res = iframeWin.getDeviceInfo();
                    if (!res) {
                        iview.Message.error("请选择一条记录");
                    } else {
                        layer.close(index);
                        vm.devLeaveRecord.devCode = res.devCode;
                        vm.devLeaveRecord.devName = res.devName;
                        vm.devLeaveRecord.devId = res.id;
                    }
                }
            })
        },
        deleteLeave: function() {
            var ids  = getSelectedRows("#jqGrid");
            if (ids == null) {
                return;
            }

            var rowDatas=getSelectedRowDatas("#jqGrid");
            //批量修改状态
            vm.List=rowDatas;
            var erArray=[];
            openWindow({
                title: "设备状态修改",
                area: ['550px', '300px'],
                content: jQuery("#reviewLayer"),
                btn: ['确定', '取消'],
                btn1: function (index) {
                    if(!vm.devLeaveRecord.status){
                        layer.alert('请选择设备状态!')
                        return
                    }
                    for (var r=0;r<rowDatas.length;r++ ){
                        rowDatas[r].status = vm.devLeaveRecord.status;
                    }
                    Ajax.request({
                        url: '../devleaverecord/batchUpdate',
                        params: JSON.stringify(rowDatas),
                        type: 'POST',
                        contentType: "application/json",
                        successCallback: function (result) {
                            alert("操作成功",function () {
                                vm.reload();
                                layer.closeAll();
                            })
                        }
                    });
                }
            });
        },
        review: function() {
            // 必须选择一条进行审核
            var ids  = getSelectedRows("#jqGrid");
            if (ids == null) {
                return;
            }
            var rowDatas = getSelectedRowDatas("#jqGrid");
            for (let i = 0; i < rowDatas.length; i++) {
                if(rowDatas[i].status != '待审核'){
                    layer.alert('设备[' + rowDatas[i].devName + ']已审核过，无需重复审核！', {icon: 5});
                    return;
                }
            }
            Ajax.request({
                url: '../devleaverecord/batchReview',
                params: JSON.stringify(ids),
                type: 'POST',
                contentType: "application/json",
                successCallback: function (result) {
                    alert("操作成功",function () {
                        vm.reload();
                        layer.closeAll();
                    })
                }
            });
        }

    }
});
